grammar XHTML2CSV_raw;


tokens {
	CELLULEG 	=  '<td>' ;   // simple lexemes
	CELLULED	=  '</td>' ;
	LIGNEG 		=  '<tr>' ;
	LIGNED		=  '</tr>' ;
	TABLED		=  '</table>' ;
}

@members {
	public static void main(String [] args) throws Exception {

		XHTML2CSV_rawLexer lex = new XHTML2CSV_rawLexer(new ANTLRFileStream(args[0]));	// create lexer to read the file specified from command line (i.e., first argument, e.g., java Main test1.xhtml)
		CommonTokenStream tokens = new CommonTokenStream(lex);					// transform it into a token stream
		XHTML2CSV_rawParser parser = new XHTML2CSV_rawParser(tokens);					// create the parser that reads from the token stream
	
		try {
			String s = parser.balise_table();// (try to) parse a given rule specified in the parser file, e.g. cellule
            System.out.println(s);

		} catch (RecognitionException e)  {
			e.printStackTrace();
		}
	}
}
 
/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/

balise_table returns [String csv]: TABLEALIGN ( position_table ) BORDER ENTIER FINBALISE m=milieu TABLED {$csv = $m.csv;} ;

position_table : GUILLEMET POS GUILLEMET;

milieu returns [String csv]:  l=ldlg m=milieu2  {$csv = $l.csv + "\n" + $m.csv;} ;

milieu2 returns [String csv]: l=ldlg m=milieu2 {$csv = $l.csv + "\n" + $m.csv;}|  {$csv = "";};

ldlg returns [String csv]: LIGNEG e=entre LIGNED {$csv = $e.csv;} ;

entre returns [String csv]: c=cellule e=entre {$csv = $c.csv + ", " + $e.csv;} | c=cellule {$csv = $c.csv ;}  ;

cellule	returns [String csv] : CELLULEG CHAINE CELLULED {$csv = "\"" + $CHAINE.text;} ;

/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/
	
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ {
				$channel = HIDDEN;
			} ;  															// skip white spaces

TABLEALIGN : ('<table align=') ;

BORDER : (' border=') ;

POS : ('center' | 'left' | 'right') ;

ENTIER : '\"' ('1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9')* '\"';

GUILLEMET : ('"') ;

FINBALISE : ('>') ;

CHAINE	:  (~('\"' | ',' | '\n' | '<' | '>'))+ ; 							// complex lexemes


